<?php
class m_calon_adik_asuh extends Model{
    var $table1 = 'adik_asuh',
        $table2 = 'dinamis_adik_asuh',
        $gallery_path,
        $thumb_path;
    function m_calon_adik_asuh(){
        parent::Model();
        $this->gallery_path = realpath(APPPATH.'../image/foto_calon_adik_asuh');
        $this->thumb_path = $this->gallery_path.'/thumbs';
    }
    function getCountRows($params){
        if($params['nama'] != NULL){
            $action = "and a.nama like ('%$params[nama]%')";
        }
        $query = $this->db->query("select a.*,da.alamat,da.no_telp,da.jumlah_saudara,s.nama as sekolah,k.nama as kelas,t.nama as tingkat_pendidikan,ta.nama as tahun_ajaran,sm.nama as semester from adik_asuh a
                                   join dinamis_adik_asuh da on a.id=da.id_adik_asuh 
                                   join sekolah s on da.id_sekolah=s.id 
                                   join kelas k on da.id_kelas=k.id 
                                   join tingkat_pendidikan t on k.id_tingkat_pendidikan=t.id 
                                   join tahun_ajaran ta on da.id_tahun_ajaran = ta.id
                                   join semester sm on ta.id_semester = sm.id
                                   where da.status='1' and a.status = '0' $action");
        
        return $query->num_rows();
    }
    function getListCalonAdikAsuh($offset = NULL,$limit = NULL,$params,$sortBy,$type){
        if($params['nama'] != NULL){
            $action = "and a.nama like ('%$params[nama]%')";
        }
        if($sortBy == 'nama'){
            $order = "order by a.nama $type";
        }
        if($offset != NULL || $limit != NULL){
            $limits = "limit $offset,$limit";
        }else $limits = '';
        
        $query = $this->db->query("select a.*,da.alamat,da.no_telp,da.jumlah_saudara,s.nama as sekolah,k.nama as kelas,t.nama as tingkat_pendidikan,ta.nama as tahun_ajaran,sm.nama as semester from adik_asuh a
                                   join dinamis_adik_asuh da on a.id=da.id_adik_asuh 
                                   join sekolah s on da.id_sekolah=s.id 
                                   join kelas k on da.id_kelas=k.id 
                                   join tingkat_pendidikan t on k.id_tingkat_pendidikan=t.id 
                                   join tahun_ajaran ta on da.id_tahun_ajaran = ta.id
                                   join semester sm on ta.id_semester = sm.id
                                   where da.status='1' and a.status = '0' $action $order $limits");
        if($query->num_rows() > 0){
            $result = $query->result_array();
        }else $result = array();
        
        return $result;
    }
    function getCalonAdikAsuhById($id){
        $query = $this->db->query("select a.*,da.alamat,da.no_telp,da.jumlah_saudara,da.gambar,da.id_sekolah,da.id_kelas,s.nama as sekolah,k.nama as kelas,t.nama as tingkat_pendidikan,ta.nama as tahun_ajaran,ta.id as id_tahun_ajaran,sm.nama as semester from adik_asuh a
                                   join dinamis_adik_asuh da on a.id=da.id_adik_asuh 
                                   join sekolah s on da.id_sekolah=s.id 
                                   join kelas k on da.id_kelas=k.id 
                                   join tingkat_pendidikan t on k.id_tingkat_pendidikan=t.id 
                                   join tahun_ajaran ta on da.id_tahun_ajaran = ta.id
                                   join semester sm on ta.id_semester = sm.id
                                   where da.status='1' and a.id='$id'");
        if($query->num_rows() > 0){
            $result = $query->result_array();
        }else $result = array();
        
        return $result;
    }
    function getDinamisCalonAdikAsuh($id = NULL){
        if($id != NULL){
          $where = "where da.id_adik_asuh='$id'";    
        }else $where = "";
        
        $query = $this->db->query('select da.*,s.nama as sekolah,k.nama as kelas,t.nama as tingkat_pendidikan,ta.nama as tahun_ajaran,sm.nama as semester from dinamis_adik_asuh da
                                  join sekolah s on da.id_sekolah = s.id 
                                  join kelas k on da.id_kelas = k.id 
                                  join tingkat_pendidikan t on k.id_tingkat_pendidikan = t.id 
                                  join tahun_ajaran ta on da.id_tahun_ajaran = ta.id
                                  join semester sm on ta.id_semester = sm.id
                                  '.$where.' order by status desc');
        if($query->num_rows() > 0){
            $result = $query->result_array();
        }else $result = array();
        
        return $result;
    }
    function addCalonAdikAsuh($data){
       $nama = $data['nama'];
       $tglLahir = date2mysql($data['tglLahir']);
       $jenKel = $data['jeKel'];
       $ortu = $data['ortu'];
       $jumSaudara = $data['jumSaudara'];
       $alamat = $data['alamat'];
       $noTelp = $data['noTelp'];
       $sekolah = $data['idSekolah'];
       $kelas = $data['kelas'];
       $idTahunAjaran = $data['id_tahun_ajaran'];
       $tanggal = date('Y-m-d');
       $status = '0';
       
       $dataGambar = $this->uploadValue($this->gallery_path);
       $this->makeThumbnail($dataGambar['full_path'],$dataGambar['image_height'],$dataGambar['image_width'],$this->thumb_path);
       
       $calonAdikAsuh = array('nama' => $nama,
           'tgl_lahir' => $tglLahir,
           'jenis_kelamin' => $jenKel,
           'ortu' => $ortu,
           'status' => $status);
       $this->db->insert($this->table1,$calonAdikAsuh);
       $id = $this->db->insert_id();
       
       $dinamisCalonAdikAsuh = array('tanggal' => $tanggal,
           'id_adik_asuh' => $id,
           'alamat' => $alamat,
           'no_telp' => $noTelp,
           'jumlah_saudara' => $jumSaudara,
           'id_sekolah' => $sekolah,
           'id_kelas' => $kelas,
           'id_tahun_ajaran' => $idTahunAjaran,
           'gambar' => $dataGambar['file_name'],
           'status' => '1');
       $insert = $this->db->insert($this->table2,$dinamisCalonAdikAsuh);
       //Jika ditambahkan calon adik asuh baru maka input tabel bobot_alternatif dengan data kriteria
       $query = $this->db->query("select id from kriteria");
       if($query->num_rows() > 0){
           $result = $query->result_array();
           foreach ($result as $res){
               $input = $this->db->query("insert into bobot_alternatif (id_kriteria,id_calon_adik_asuh,bobot) values ('$res[id]','$id',0)");
           }
       }
       if(!$insert){
           return FALSE;
       }else return TRUE;
    }
    function editCalonAdikAsuh($data){
       $id = $data['id']; 
       $nama = $data['nama'];
       $tglLahir = date2mysql($data['tglLahir']);
       $jenKel = $data['jeKel'];
       $ortu = $data['ortu'];
       $jumSaudara = $data['jumSaudara'];
       $alamat = $data['alamat'];
       $noTelp = $data['noTelp'];
       $sekolah = $data['idSekolah'];
       $idTahunAjaran = $data['id_tahun_ajaran'];
       $kelas = $data['kelas'];
       
       $dataGambar = $this->uploadValue($this->gallery_path);
       $this->makeThumbnail($dataGambar['full_path'],$dataGambar['image_height'],$dataGambar['image_width'],$this->thumb_path);
       
       $calonAdikAsuh = array('nama' => $nama,
           'tgl_lahir' => $tglLahir,
           'jenis_kelamin' => $jenKel,
           'ortu' => $ortu);
       
       $this->db->where('id',$id);
       $this->db->update($this->table1,$calonAdikAsuh);
       
       $dinamisCalonAdikAsuh['alamat'] = $alamat;
       $dinamisCalonAdikAsuh['no_telp'] = $noTelp;
       $dinamisCalonAdikAsuh['jumlah_saudara'] = $jumSaudara;
       $dinamisCalonAdikAsuh['id_sekolah'] = $sekolah;
       $dinamisCalonAdikAsuh['id_kelas'] = $kelas;
       $dinamisCalonAdikAsuh['id_tahun_ajaran'] = $idTahunAjaran;
       $dinamisCalonAdikAsuh['alamat'] = $alamat;
       if($dataGambar['file_name'] != ''){
           $dinamisCalonAdikAsuh['gambar'] = $dataGambar['file_name'];
       }
       
       $this->db->where('id_adik_asuh',$id);
       $this->db->where('status','1');
       $update = $this->db->update($this->table2,$dinamisCalonAdikAsuh);
       
       if(!$update){
           return FALSE;
       }else return TRUE;
    }
    function addDinamisCalonAdikAsuh($data){
        $dataGambar = $this->uploadValue($this->gallery_path);
        $this->makeThumbnail($dataGambar['full_path'],$dataGambar['image_height'],$dataGambar['image_width'],$this->thumb_path);
        
        $data['gambar'] = $dataGambar['file_name'];
        
        $this->db->where('id_adik_asuh',$data['id_adik_asuh']);
        $this->db->update($this->table2,array('status' => '0'));
        
        $insert = $this->db->insert($this->table2,$data);
        if(!$insert){
            return FALSE;
        }else return TRUE;
    }
    function deleteCalonAdikAsuh($id){
        $delete = $this->db->delete($this->table1,array('id'=>$id));
        if(!$delete){
            return FALSE;
        }else return TRUE;
    }
    /*
     * mengembalikan data file yg diupload
     */
    function uploadValue($gallery_path){
        $config['upload_path'] = $this->gallery_path;
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']	= '2000';
        
        $this->upload->initialize($config);
        $this->upload->do_upload();
        
        return $dataGambar = $this->upload->data();
    }
    //--------------------------------------------------------------------------
    /*
     * membuat gambar thumbnail pada file yg diupload
     */
    function makeThumbnail($fullPath,$imageHeight,$imageWidth,$thumbPath){
       $widthThumb = 150;
       $heightThumb = ceil(($widthThumb * $imageHeight)/$imageWidth);
       
       $config = array(
       'source_image'=> $fullPath,
       'new_image'=> $thumbPath,
       'maintain_ration'=>true,
       'width'=>$widthThumb,
       'height'=>$heightThumb
       );

       $this->load->library('image_lib', $config);
       $this->image_lib->resize();
    }
    //--------------------------------------------------------------------------
}
?>
